import axios from 'axios';

// 创建axios实例
const http = axios.create({
  baseURL: 'http://localhost:8082',
  headers: {
    'Content-Type': 'application/json'
  }
});

// 添加请求拦截器
http.interceptors.request.use(config => {
  const token = localStorage.getItem('token');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
}, error => {
  return Promise.reject(error);
});

// 添加响应拦截器
http.interceptors.response.use(response => {
  return response;
}, error => {
  // 统一处理错误
  if (error.response) {
    switch (error.response.status) {
      case 401:
        // 未授权，可以在这里处理登出逻辑
        localStorage.removeItem('token');
        break;
      case 403:
        // 禁止访问
        break;
      case 500:
        // 服务器错误
        break;
    }
  }
  return Promise.reject(error);
});

export default http;